package com.stock;

import com.kevin.entity.DayK;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
import org.codehaus.jackson.type.JavaType;
import org.codehaus.jackson.type.TypeReference;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class JsonUtils {
	
	private static ObjectMapper mapper = new ObjectMapper();  
	
	public static String obj2json(Object o){
        try {
			return mapper.writeValueAsString(o);
		} catch (JsonGenerationException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}  
        return null;
	}
	
	public static <T> T json2obj(String json,Class<T> clazz){
		ObjectMapper mapper = new ObjectMapper();  
		try {
			return mapper.readValue(json,clazz);  
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static <T> List<T> json2list(String json,Class<T> clazz){
		List<T> list = null;
		try {
			JavaType collectionType = getCollectionType(List.class,clazz);
		 list =mapper.readValue(json,collectionType);
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return list;   
	}
	
	public static String obj2jsonf(Object o){
		mapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, Boolean.TRUE);
		try {
			return mapper.writeValueAsString(o);
		} catch (JsonGenerationException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static void printf(Object o){
		String obj2jsonf = obj2jsonf(o);
		System.out.println(obj2jsonf);
	}
	
	
	 /**   
     * 获取泛型的Collection Type  
     * @param collectionClass 泛型的Collection   
     * @param elementClasses 元素类   
     * @return JavaType Java类型   
     * @since 1.0   
     */   
 public static JavaType getCollectionType(Class<?> collectionClass, Class<?>... elementClasses) {   
     return mapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);   
 }
	
	public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
		String json="[{\"id\":\"1\"},{\"id\":\"2\"}]";
		ObjectMapper mapper = new ObjectMapper();
		List<DayK> beanList = mapper.readValue(json, new TypeReference<List<DayK>>() {}); 
		System.out.println(Arrays.toString(beanList.toArray()));
		
		DayK k1 = new DayK();
		k1.setId(1l);
		DayK k2 = new DayK();
		k2.setId(2l);
		List<DayK> list=new ArrayList<DayK>();
		list.add(k1);
		list.add(k2);
		System.out.println(obj2json(list));

		List<DayK> json2list = json2list(json,DayK.class);
		System.out.println(Arrays.toString(json2list.toArray()));
		
	}

}
